package com.ejlchina.tool.fragment.offline

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentDeviceModeSelectBinding
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.constant.DEVICE_MODE_NO_SELECT
import com.ejlchina.tool.constant.DEVICE_MODE_OFFLINE
import com.ejlchina.tool.constant.DEVICE_MODE_ONLINE
import com.ejlchina.tool.constant.SP_DEVICE_MODE
import com.ejlchina.tool.extension.safeNavigate
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.singleton.SharedPreferenceUtil
import com.huyuhui.fastble.BleManager

class DeviceModeSelectFragment: BaseBindingFragment2<FragmentDeviceModeSelectBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setActionBarPadding(false)
        checkDeviceMode()
        setHeadVisibility(View.GONE)
//        checkCurrentDeviceMode()
        initListeners()
    }

    @SuppressLint("MissingPermission")
    private fun initListeners(){
        binding.layoutDragon.setOnClickListener {
            SharedPreferenceUtil.putInt(SP_DEVICE_MODE, DEVICE_MODE_ONLINE)
            findNavController().safeNavigate(R.id.action_device_mode_to_check_index_fix)
            BleManager.disconnectAllDevice()
            GlobalConnectObject.getCurrentConnectDeviceList().clear()
        }
        binding.layoutBird.setOnClickListener {
            SharedPreferenceUtil.putInt(SP_DEVICE_MODE, DEVICE_MODE_OFFLINE)
            findNavController().safeNavigate(R.id.action_device_mode_to_offline_config)
            BleManager.disconnectAllDevice()
            GlobalConnectObject.getCurrentConnectDeviceList().clear()
        }
    }

    private fun checkCurrentDeviceMode(){
        when(SharedPreferenceUtil.getInt(SP_DEVICE_MODE,-1)){
            DEVICE_MODE_ONLINE -> {
                findNavController().safeNavigate(R.id.action_device_mode_to_check_index_fix)
            }
            DEVICE_MODE_OFFLINE -> {
                findNavController().safeNavigate(R.id.action_device_mode_to_offline_config)
            }
        }
    }

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentDeviceModeSelectBinding {
        return FragmentDeviceModeSelectBinding.inflate(inflater,container,true)
    }
}